﻿using EntityFramework.Extensions;
using LJSheng.Common;
using LJSheng.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace LJSheng.Web.Controllers
{
    public class ljshengController : AuthenticationController
    {
        #region 后台管理
        // 后台操作提示
        public ActionResult msg()
        {
            ViewBag.msg = Request.QueryString["msg"];
            ViewBag.title = Request.QueryString["title"];
            ViewBag.url = Request.QueryString["url"];
            ViewBag.href = Request.QueryString["href"];
            return View();
        }
        // 后台登录
        public ActionResult dl()
        {
            return View();
        }
        // 后台
        public ActionResult houtai()
        {
            return View();
        }
        // 菜单
        //[Authorize(Roles = "admins")]
        public ActionResult caidan()
        {
            using (EFDB db = new EFDB())
            {
                var t = db.LJSheng.Where(l => l.account == "token" && l.explain != "").FirstOrDefault();
                ViewBag.token = "9021616";
                if (t != null)
                {
                    ViewBag.token = t.explain;
                }
            }
            return View();
        }
        // 头部
        public ActionResult toubu()
        {
            return View();
        }

        /// <summary>
        /// 头部数据
        /// </summary>
        [HttpPost]
        public JsonResult tb()
        {
            StringBuilder sb = new StringBuilder("SELECT TOP 1 ");
            sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=2) AS dd,");
            sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=1) AS pay,");
            sb.Append("(SELECT SUM(user_cash) FROM LOrder) AS rmb,");
            //sb.Append("(SELECT COUNT(pay_status) FROM LOrder WHERE pay_status=1 AND addtime >= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 00:00:00' AND addtime <= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 23:59:59" + "') AS ddzr,");
            //sb.Append("(SELECT COUNT(pay_status) FROM Pay) AS tx,");
            //sb.Append("(SELECT COUNT(pay_status) FROM Pay WHERE pay_status=2) AS txtx,");
            sb.Append("(SELECT COUNT(gid) FROM ShopList WHERE show!=3) AS sj,");
            sb.Append("(SELECT COUNT(gid) FROM LUser) AS hy");
            //sb.Append("(SELECT COUNT(gid) FROM LUser WHERE lx!=3 AND DATEDIFF(day,addtime,'" + DateTime.Now + "')=0) AS hyjr,");
            //sb.Append("(SELECT COUNT(gid) FROM LUser WHERE lx!=3 AND addtime >= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 00:00:00' AND addtime <= '" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " 23:59:59" + "') AS hyzr");
            sb.Append(" FROM LOrder");
            DataTable dt = DbHelperSQL.Query(sb.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                //ViewBag.dd = dt.Rows[0]["dd"].ToString();
                //ViewBag.ddjr = dt.Rows[0]["ddjr"].ToString();
                //ViewBag.ddzr = dt.Rows[0]["ddzr"].ToString();
                //ViewBag.tx = dt.Rows[0]["tx"].ToString();
                //ViewBag.txtx = dt.Rows[0]["txtx"].ToString();
                //ViewBag.sj = dt.Rows[0]["sj"].ToString();
                //ViewBag.hy = dt.Rows[0]["hy"].ToString();
                //ViewBag.hyjr = dt.Rows[0]["hyjr"].ToString();
                //ViewBag.hyzr = dt.Rows[0]["hyzr"].ToString();
                using (EFDB db = new EFDB())
                {
                    return Json(new AjaxResult(new
                    {
                        dd = dt.Rows[0]["dd"].ToString(),
                        pay = dt.Rows[0]["pay"].ToString(),
                        rmb = dt.Rows[0]["rmb"].ToString(),
                        //ddjr = dt.Rows[0]["ddjr"].ToString(),
                        //ddzr = dt.Rows[0]["ddzr"].ToString(),
                        //tx = dt.Rows[0]["tx"].ToString(),
                        //txtx = dt.Rows[0]["txtx"].ToString(),
                        sj = dt.Rows[0]["sj"].ToString(),
                        hy = dt.Rows[0]["hy"].ToString()
                        //hyjr = dt.Rows[0]["hyjr"].ToString(),
                        //hyzr = dt.Rows[0]["hyzr"].ToString()
                    }));
                }

            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        // 后台导航
        public ActionResult title()
        {
            return View();
        }
        // 后台中心
        public ActionResult index()
        {
            int all = 0;
            using (EFDB db = new EFDB())
            {
                var b = db.LUser.Select(l => new User { gid = l.gid, usergid = l.usergid }).ToList();
                foreach (var dr in b)
                {
                    int n = Helper.fs(b, dr.gid, 3);
                    all += n;
                    db.LUser.Where(l => l.gid == dr.gid).Update(l => new LUser { number = n });
                }
            }
            return View(all);
        }
        #endregion

        #region 会员模块
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult hyau()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    LUser b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.account = b.account;
                    ViewBag.pwd = b.pwd;
                    ViewBag.real_name = b.real_name;
                    ViewBag.sex = b.sex;
                    ViewBag.rmb = b.rmb;
                    ViewBag.zk = b.zk;
                    ViewBag.explain = b.explain;
                    ViewBag.nickname = b.nickname;
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.area = b.area;
                    ViewBag.sfz = b.sfz;
                    ViewBag.openid = b.openid;
                    ViewBag.qx = b.qx;
                    ViewBag.grade = b.grade;
                    ViewBag.khh = b.khh;
                    ViewBag.khm = b.khm;
                    ViewBag.kahao = b.kahao;
                    var u = db.LUser.Where(l => l.gid == b.usergid).FirstOrDefault();
                    if (u != null)
                    {
                        ViewBag.user = u != null ? u.account : "顶级用户";
                        ViewBag.uname = u.real_name;
                    }
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult hyau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                LUser b;
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                string real_name = Request.Form["real_name"];
                string sex = Request.Form["sex"];
                decimal rmb = decimal.Parse(Request.Form["rmb"]);
                decimal zk = decimal.Parse(Request.Form["zk"]);
                string explain = Request.Form["explain"];
                string nickname = Request.Form["nickname"];
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                string area = Request.Form["area"];
                string sfz = Request.Form["sfz"];
                string khh = Request.Form["khh"];
                string khm = Request.Form["khm"];
                string kahao = Request.Form["kahao"];
                string qx = Request.Form["qx"];
                if (db.LUser.Where(l => l.account == account && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("操作失败！", "history.go(-1);", "帐号已存在!");
                }
                else
                {
                    if (gid == null)
                    {
                        b = new LUser();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                        b.account = account;
                        b.ip = Helper.IP;
                        b.grade = "";
                        b.usergid = null;
                        b.lx = 1;
                        b.number = 0;
                    }
                    else
                    {
                        b = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    b.real_name = real_name;
                    if (b.pwd != pwd)
                    {
                        b.pwd = Common.MD5.GetMD5ljsheng(pwd);
                    }
                    b.qx = qx;
                    b.sex = sex == "男" ? 1 : 2;
                    b.nickname = nickname;
                    b.province = province;
                    b.city = city;
                    b.area = area;
                    b.sfz = sfz;
                    b.khh = khh;
                    b.khm = khm;
                    b.kahao = kahao;
                    b.rmb = rmb;
                    b.zk = zk;
                    b.explain = explain;
                    b.loginid = Guid.NewGuid().ToString();
                    if (gid == null)
                    {
                        db.LUser.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        return Helper.WebRedirect("操作成功！", "/ljsheng/hylist", "恭喜你,操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }

        }

        // 列表管理
        //同时支持Get和Post
        //[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult hylist()
        {
            if (Request.Form["daochu"] == "daochu")
            {
                using (EFDB db = new EFDB())
                {
                    string province = LCookie.Getgly("province");
                    string city = LCookie.Getgly("city");
                    string area = LCookie.Getgly("area");
                    string stime = Request.Form["stime"];
                    string etime = Request.Form["etime"];
                    string pstime = Request.Form["pstime"];
                    string petime = Request.Form["petime"];
                    string account = Request.Form["account"];
                    string real_name = Request.Form["real_name"];

                    var b = db.LUser.Where(l => l.grade != "注册套餐").AsQueryable();
                    if (!string.IsNullOrEmpty(account))
                    {
                        b = b.Where(l => l.account.Contains(account));
                    }
                    if (!string.IsNullOrEmpty(real_name))
                    {
                        b = b.Where(l => l.real_name.Contains(real_name));
                    }
                    if (!string.IsNullOrEmpty(province))
                    {
                        b = b.Where(l => l.province.Contains(province));
                    }
                    if (!string.IsNullOrEmpty(city))
                    {
                        b = b.Where(l => l.city.Contains(city));
                    }
                    if (!string.IsNullOrEmpty(area))
                    {
                        b = b.Where(l => l.area.Contains(area));
                    }
                    //时间查询
                    if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                    {
                        DateTime? st = null;
                        DateTime? et = null;
                        if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            st = DateTime.Parse(stime);
                            et = DateTime.Parse(etime);
                        }
                        b = b.Where(l => l.addtime >= st && l.addtime <= et);
                    }
                    //收入时间查询
                    DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                    DateTime? pet = DateTime.Now;
                    if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                    {
                        if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            pst = DateTime.Parse(pstime);
                            pet = DateTime.Parse(petime);
                        }
                    }
                    //获取list数据
                    var list = b.OrderByDescending(l => l.addtime).Select(l => new
                    {
                        l.gid,
                        l.province,
                        l.city,
                        l.area,
                        l.account,
                        l.grade,
                        l.picture,
                        l.sfzpicture,
                        l.real_name,
                        l.nickname,
                        l.rmb,
                        l.zk,
                        l.explain,
                        //rmb = db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0,
                        l.addtime,
                        user = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        ureal_name = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name
                    }).ToList();

                    //创建Excel文件的对象
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //添加一个sheet
                    NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                    //给sheet1添加第一行的头部标题
                    NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                    row1.CreateCell(0).SetCellValue("注册区域");
                    row1.CreateCell(1).SetCellValue("会员帐号");
                    row1.CreateCell(2).SetCellValue("真实姓名");
                    row1.CreateCell(3).SetCellValue("会员等级");
                    row1.CreateCell(4).SetCellValue("推荐人");
                    row1.CreateCell(5).SetCellValue("收入");
                    row1.CreateCell(6).SetCellValue("注册时间");
                    //将数据逐步写入sheet1各个行
                    int i = 0;
                    for (i = 0; i < list.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                        rowtemp.CreateCell(0).SetCellValue(list[i].city + list[i].area);
                        rowtemp.CreateCell(1).SetCellValue(list[i].account);
                        rowtemp.CreateCell(2).SetCellValue(list[i].real_name);
                        rowtemp.CreateCell(3).SetCellValue(list[i].grade);
                        rowtemp.CreateCell(4).SetCellValue(list[i].user + list[i].ureal_name);
                        rowtemp.CreateCell(5).SetCellValue(list[i].rmb.ToString());
                        rowtemp.CreateCell(6).SetCellValue(list[i].addtime.ToString());
                    }
                    NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                    //统计
                    decimal parame1 = 0;
                    if (b.Count() > 0)
                    {
                        parame1 = b.GroupJoin(db.Pay,
                        x => x.gid,
                        y => y.usergid,
                        (x, y) => new
                        {
                            rmb = db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                        }).Sum(l => l.rmb);
                    }
                    rowend.CreateCell(1).SetCellValue("当前查询条件提现总金额=" + parame1);
                    // 写入到客户端 
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    book.Write(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms, "application/vnd.ms-excel", "会员.xls");
                }
            }
            else
            {
                return View();
            }
        }
        [HttpPost]
        public ActionResult ahylist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string province = LCookie.Getgly("province");
                string city = LCookie.Getgly("city");
                string area = LCookie.Getgly("area");
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string pstime = paramJson["pstime"].ToString();
                string petime = paramJson["petime"].ToString();
                string account = paramJson["account"].ToString();
                string real_name = paramJson["real_name"].ToString();
                var b = db.LUser.Where(l => l.grade != "注册套餐").AsQueryable();
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(real_name))
                {
                    b = b.Where(l => l.real_name.Contains(real_name));
                }
                if (!string.IsNullOrEmpty(province))
                {
                    b = b.Where(l => l.province.Contains(province));
                }
                if (!string.IsNullOrEmpty(city))
                {
                    b = b.Where(l => l.city.Contains(city));
                }
                if (!string.IsNullOrEmpty(area))
                {
                    b = b.Where(l => l.area.Contains(area));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                //收入时间查询
                DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                DateTime? pet = DateTime.Now;
                if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                {
                    if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        pst = DateTime.Parse(pstime);
                        pet = DateTime.Parse(petime);
                    }
                }
                decimal parame1 = 0;
                if (b.Count() > 0)
                {
                    parame1 = b.GroupJoin(db.Pay,
                    x => x.gid,
                        y => y.usergid,
                        (x, y) => new
                        {
                            rmb = db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                        }).Sum(l => l.rmb);
                }
                //decimal parame1 = db.Pay.Where(p => p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p=>p.lx!=3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).Select(l => new
                    {
                        l.gid,
                        l.province,
                        l.city,
                        l.area,
                        l.account,
                        l.grade,
                        l.picture,
                        l.sfzpicture,
                        l.real_name,
                        l.nickname,
                        l.rmb,
                        l.zk,
                        l.explain,
                        //rmb = db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0,
                        l.addtime,
                        l.number,
                        user = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        ureal_name = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name
                    }).ToList()
                }));
            }
        }

        #region 分销页面

        public ActionResult fx()
        {
            using (EFDB db = new EFDB())
            {
                if (Request.Form["daochu"] == "daochu")
                {
                    string stime = Request.Form["stime"];
                    string etime = Request.Form["etime"];
                    string pstime = Request.Form["pstime"];
                    string petime = Request.Form["petime"];
                    string account = Request.Form["account"];
                    string real_name = Request.Form["real_name"];
                    Guid gid = Guid.Parse(Request.Form["gid"]);
                    var b = db.LUser.Where(l => l.usergid == gid).AsQueryable();
                    if (!string.IsNullOrEmpty(account))
                    {
                        b = b.Where(l => l.account.Contains(account));
                    }
                    if (!string.IsNullOrEmpty(real_name))
                    {
                        b = b.Where(l => l.real_name.Contains(real_name));
                    }
                    //时间查询
                    if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                    {
                        DateTime? st = null;
                        DateTime? et = null;
                        if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            st = DateTime.Parse(stime);
                            et = DateTime.Parse(etime);
                        }
                        b = b.Where(l => l.addtime >= st && l.addtime <= et);
                    }
                    //收入时间查询
                    DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                    DateTime? pet = DateTime.Now;
                    if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                    {
                        if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            pst = DateTime.Parse(pstime);
                            pet = DateTime.Parse(petime);
                        }
                    }
                    //获取list数据
                    var list = b.OrderByDescending(l => l.addtime).Select(l => new
                    {
                        l.gid,
                        l.province,
                        l.city,
                        l.area,
                        l.account,
                        l.grade,
                        l.picture,
                        l.sfzpicture,
                        l.real_name,
                        l.nickname,
                        rmb = db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0,
                        l.addtime,
                        user = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        ureal_name = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name
                    }).ToList();

                    //创建Excel文件的对象
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //添加一个sheet
                    NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                    //给sheet1添加第一行的头部标题
                    NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                    row1.CreateCell(0).SetCellValue("注册区域");
                    row1.CreateCell(1).SetCellValue("会员帐号");
                    row1.CreateCell(2).SetCellValue("真实姓名");
                    row1.CreateCell(3).SetCellValue("会员等级");
                    row1.CreateCell(4).SetCellValue("推荐人");
                    row1.CreateCell(5).SetCellValue("收入");
                    row1.CreateCell(6).SetCellValue("注册时间");
                    //将数据逐步写入sheet1各个行
                    int i = 0;
                    for (i = 0; i < list.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                        rowtemp.CreateCell(0).SetCellValue(list[i].city + list[i].area);
                        rowtemp.CreateCell(1).SetCellValue(list[i].account);
                        rowtemp.CreateCell(2).SetCellValue(list[i].real_name);
                        rowtemp.CreateCell(3).SetCellValue(list[i].grade);
                        rowtemp.CreateCell(4).SetCellValue(list[i].user + list[i].ureal_name);
                        rowtemp.CreateCell(5).SetCellValue(list[i].rmb.ToString());
                        rowtemp.CreateCell(6).SetCellValue(list[i].addtime.ToString());
                    }
                    NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                    //统计
                    decimal parame1 = 0;
                    if (b.Count() > 0)
                    {
                        parame1 = b.GroupJoin(db.Pay,
                        x => x.gid,
                        y => y.usergid,
                        (x, y) => new
                        {
                            rmb = db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                        }).Sum(l => l.rmb);
                    }
                    rowend.CreateCell(1).SetCellValue("当前查询条件提现总金额=" + parame1);
                    // 写入到客户端 
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    book.Write(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms, "application/vnd.ms-excel", "会员.xls");
                }
                else
                {
                    ViewBag.sj = Request.QueryString["account"] + "(" + Request.QueryString["real_name"] + ")";
                    return View();
                }
            }
        }
        [HttpPost]
        public ActionResult afxlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string pstime = paramJson["pstime"].ToString();
                string petime = paramJson["petime"].ToString();
                string account = paramJson["account"].ToString();
                string real_name = paramJson["real_name"].ToString();
                Guid gid = Guid.Parse(paramJson["gid"].ToString());
                var b = db.LUser.Where(l => l.usergid == gid).AsQueryable();
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(real_name))
                {
                    b = b.Where(l => l.real_name.Contains(real_name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                //收入时间查询
                DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                DateTime? pet = DateTime.Now;
                if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                {
                    if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        pst = DateTime.Parse(pstime);
                        pet = DateTime.Parse(petime);
                    }
                }
                decimal parame1 = 0;
                if (b.Count() > 0)
                {
                    parame1 = b.GroupJoin(db.Pay,
                    x => x.gid,
                        y => y.usergid,
                        (x, y) => new
                        {
                            rmb = db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                        }).Sum(l => l.rmb);
                }
                //decimal parame1 = db.Pay.Where(p => p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p=>p.lx!=3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).Select(l => new
                    {
                        l.gid,
                        l.province,
                        l.city,
                        l.area,
                        l.account,
                        l.grade,
                        l.picture,
                        l.sfzpicture,
                        l.real_name,
                        l.nickname,
                        rmb = db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx != 3 && p.usergid == l.gid && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0,
                        l.addtime,
                        user = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        ureal_name = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name
                    }).ToList()
                }));
            }
        }

        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult hydelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.LUser.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        /// <summary>
        /// 重设密码
        /// </summary>
        [HttpPost]
        public JsonResult hymima(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    string pwd = RandStr.CreateValidateNumber(6);
                    var pwdmm = db.LUser.Where(l => l.gid == gid).FirstOrDefault();
                    pwdmm.pwd = MD5.GetMD5ljsheng(pwd);
                    pwdmm.loginid = Common.LCommon.TimeToUNIX(DateTime.Now);
                    if (db.SaveChanges() == 1)
                    {
                        return Json(new AjaxResult("新密码为:" + pwd));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 管理员模块
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult glyau()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.LJSheng.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.account = b.account;
                    ViewBag.pwd = b.pwd;
                    ViewBag.real_name = b.real_name;
                    ViewBag.sex = b.sex;
                    ViewBag.qx = b.qx;
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.area = b.area;
                    ViewBag.zk = b.zk;
                    ViewBag.rmb = b.rmb;
                    ViewBag.explain = b.explain;
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult glyau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                Data.LJSheng b;
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                string real_name = Request.Form["real_name"];
                string sex = Request.Form["sex"];
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                string area = Request.Form["area"];
                string qx = Request.Form["qx"];
                string zk = Request.Form["zk"];
                string rmb = Request.Form["rmb"];
                string explain = Request.Form["explain"];
                if (db.LJSheng.Where(l => l.account == account && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("操作失败！", "history.go(-1);", "帐号已存在!");
                }
                else
                {
                    if (gid == null)
                    {
                        b = new Data.LJSheng();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                        b.account = account;
                    }
                    else
                    {
                        b = db.LJSheng.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    b.real_name = real_name;
                    if (b.pwd != pwd)
                    {
                        b.pwd = Common.MD5.GetMD5ljsheng(pwd);
                    }
                    b.sex = sex == "男" ? 1 : 2;
                    b.province = province == "请选择省份" ? "" : province;
                    b.city = city == "请选择城市" ? "" : city;
                    b.area = area == "请选择区域" ? "" : area;
                    b.zk = double.Parse(zk);
                    b.rmb = double.Parse(rmb);
                    b.explain = explain;
                    b.qx = qx;
                    b.loginid = Guid.NewGuid().ToString();
                    if (gid == null)
                    {
                        db.LJSheng.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        return Helper.WebRedirect("操作成功！", "/ljsheng/glylist", "恭喜你,操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }
        }

        // 列表管理
        public ActionResult glylist()
        {
            using (EFDB db = new EFDB())
            {
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string account = ViewBag.account = Request.Form["account"];
                string real_name = ViewBag.real_name = Request.Form["real_name"];
                var b = db.LJSheng.AsQueryable();
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(real_name))
                {
                    b = b.Where(l => l.real_name.Contains(real_name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                return View(b.OrderBy(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult glydelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.LJSheng.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        /// <summary>
        /// 重设密码
        /// </summary>
        [HttpPost]
        public JsonResult glymima(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    string pwd = RandStr.CreateValidateNumber(6);
                    var pwdmm = db.LJSheng.Where(l => l.gid == gid).FirstOrDefault();
                    pwdmm.pwd = MD5.GetMD5ljsheng(pwd);
                    if (db.SaveChanges() == 1)
                    {
                        return Json(new AjaxResult("新密码为:" + pwd));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 商家分类模块
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult flau()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.CPFL.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.name = b.name;
                    ViewBag.sort = b.sort;
                    ViewBag.show = b.show;
                }
            }
            else
            {
                ViewBag.sort = 1;
            }
            return View();
        }
        [HttpPost]
        public ActionResult flau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                CPFL b;
                string name = Request.Form["name"];
                int sort = Int32.Parse(Request.Form["sort"]);
                string show = Request.Form["show"];
                if (db.CPFL.Where(l => l.name == name && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("名称已存在！", "history.go(-1);", "名称已存在");
                }
                else
                {
                    if (gid == null)
                    {
                        b = new CPFL();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                    }
                    else
                    {
                        b = db.CPFL.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    b.name = name;
                    b.sort = sort;
                    b.show = show == "显示" ? 1 : 2;
                    if (gid == null)
                    {
                        db.CPFL.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        return Helper.WebRedirect("操作成功！", "history.go(-1);", "操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败,请检查录入的数据！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }
        }

        // 列表管理
        public ActionResult fllist()
        {
            using (EFDB db = new EFDB())
            {
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string name = ViewBag.name = Request.Form["name"];
                var b = db.CPFL.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                return View(b.OrderBy(l => l.sort).OrderBy(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult fldelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.CPFL.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 注册套餐
        public ActionResult buyau()
        {
            using (EFDB db = new EFDB())
            {
                if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.name = b.name;
                    ViewBag.yjrmb = b.yjrmb;
                    ViewBag.payrmb = b.payrmb;
                }
                return View(db.CPFL.ToList());
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult buyau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                int yjrmb = Int32.Parse(Request.Form["yjrmb"]);
                int payrmb = Int32.Parse(Request.Form["payrmb"]);
                var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                b.yjrmb = yjrmb;
                b.payrmb = payrmb;
                if (db.SaveChanges() == 1)
                {
                    return Helper.WebRedirect("操作成功！", "history.go(-1);", "操作成功!");
                }
                else
                {
                    return Helper.WebRedirect("失败！", "history.go(-1);", "操作失败,请重试!");
                }
            }
        }

        // 列表管理
        public ActionResult buylist()
        {
            using (EFDB db = new EFDB())
            {
                string province = LCookie.Getgly("province");
                string city = LCookie.Getgly("city");
                string area = LCookie.Getgly("area");
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string pstime = ViewBag.pstime = Request.Form["pstime"];
                string petime = ViewBag.petime = Request.Form["petime"];
                string name = ViewBag.name = Request.Form["name"];
                ViewBag.cpfl = db.CPFL.ToList();
                Guid? flgid = null;
                if (!string.IsNullOrEmpty(Request.QueryString["flgid"]) && Request.QueryString["flgid"] != "0")
                {
                    flgid = ViewBag.flgid = Guid.Parse(Request.QueryString["flgid"]);
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request.Form["flgid"]) && Request.Form["flgid"] != "0")
                    {
                        flgid = ViewBag.flgid = Guid.Parse(Request.Form["flgid"]);
                    }
                }
                var b = db.ShopList.AsQueryable();
                if (!string.IsNullOrEmpty(province))
                {
                    b = b.Where(l => l.province.Contains(province));
                }
                if (!string.IsNullOrEmpty(city))
                {
                    b = b.Where(l => l.city.Contains(city));
                }
                if (!string.IsNullOrEmpty(area))
                {
                    b = b.Where(l => l.area.Contains(area));
                }
                //不查询注册套餐
                if (Request.QueryString["lx"] == "3")
                {
                    b = b.Where(l => l.grade == "注册套餐");
                }
                else
                {
                    b = b.Where(l => l.grade != "注册套餐");
                }
                //产品分类
                if (flgid != null)
                {
                    b = b.Where(l => l.flgid == flgid);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                //收入时间查询
                DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                DateTime? pet = DateTime.Now;
                if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                {
                    if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        pst = DateTime.Parse(pstime);
                        pet = DateTime.Parse(petime);
                    }
                }
                decimal parame1 = 0;
                if (b.Count() > 0)
                {
                    parame1 = ViewBag.parame1 = b.GroupJoin(db.Pay,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        rmb = db.Pay.Where(p => p.usergid == x.usergid && p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.usergid && p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                    }).Sum(l => l.rmb);
                }
                //decimal parame1 = ViewBag.parame1 = db.Pay.Where(p => p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0;
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                if (Request.Form["daochu"] == "daochu")
                {
                    {
                        var list = b.ToList();
                        //创建Excel文件的对象
                        NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                        //添加一个sheet
                        NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                        //给sheet1添加第一行的头部标题
                        NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                        row1.CreateCell(0).SetCellValue("商家名称");
                        row1.CreateCell(1).SetCellValue("商家帐号");
                        row1.CreateCell(2).SetCellValue("商家等级");
                        row1.CreateCell(3).SetCellValue("收入");
                        row1.CreateCell(4).SetCellValue("注册时间");
                        //将数据逐步写入sheet1各个行
                        int i = 0;
                        for (i = 0; i < list.Count; i++)
                        {
                            NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                            rowtemp.CreateCell(0).SetCellValue(list[i].name);
                            rowtemp.CreateCell(1).SetCellValue(list[i].account);
                            rowtemp.CreateCell(2).SetCellValue(list[i].grade);
                            rowtemp.CreateCell(3).SetCellValue(Helper.GetShopRMB(list[i].usergid, pstime, petime));
                            rowtemp.CreateCell(4).SetCellValue(list[i].addtime.ToString());
                        }
                        NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                        //统计
                        rowend.CreateCell(1).SetCellValue("当前查询条件商家营业额=" + parame1);
                        // 写入到客户端 
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        book.Write(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        return File(ms, "application/vnd.ms-excel", "商家.xls");
                    }
                }
                else
                {
                    return View(b.OrderBy(l => l.sort).OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
                }
            }
        }
        #endregion

        #region 商家模块
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult cpau()
        {
            using (EFDB db = new EFDB())
            {
                if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.name = b.name;
                    ViewBag.picture = b.picture;
                    ViewBag.rmb = b.rmb;
                    ViewBag.explain = b.explain;
                    ViewBag.introduce = b.introduce;
                    ViewBag.sort = b.sort;
                    ViewBag.show = b.show;
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.area = b.area;
                    ViewBag.address = b.address;
                    ViewBag.openid = b.openid;
                    ViewBag.qx = b.qx;
                    ViewBag.grade = b.grade;
                    ViewBag.account = b.account;
                    ViewBag.pwd = b.pwd;
                    ViewBag.xfx = b.xfx;
                    ViewBag.xfd = b.xfd;
                    ViewBag.tese = b.tese;
                    ViewBag.zhekou = b.zhekou;
                    ViewBag.fl = b.fl;
                    ViewBag.lxfs = b.lxfs;
                    ViewBag.khh = b.khh;
                    ViewBag.khm = b.khm;
                    ViewBag.kahao = b.kahao;
                    ViewBag.yjrmb = b.yjrmb;
                    ViewBag.payrmb = b.payrmb;
                    var u = db.LUser.Where(l => l.gid == b.usergid).GroupJoin(db.LUser,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        y.FirstOrDefault().account,
                        y.FirstOrDefault().real_name
                    }).FirstOrDefault();
                    if (u != null)
                    {
                        ViewBag.tjr = u.account;
                        ViewBag.uname = u.real_name;
                    }
                }
                else
                {
                    ViewBag.sort = 1;
                    ViewBag.fl = 0;
                    ViewBag.yjrmb = 0;
                    ViewBag.payrmb = 0;
                    ViewBag.qx = "正常";
                }
                return View(db.CPFL.ToList());
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult cpau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                ShopList b;
                string picture = Request.Form["pictureurl"];
                string name = Request.Form["name"];
                string explain = Request.Form["explain"];
                string introduce = Request.Form["introduce"];
                int sort = Int32.Parse(Request.Form["sort"]);
                string show = Request.Form["show"];
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                string area = Request.Form["area"];
                string address = Request.Form["address"];
                string openid = Request.Form["openid"];
                string grade = Request.Form["grade"];
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                int xfx = Int32.Parse(Request.Form["xfx"]);
                int xfd = Int32.Parse(Request.Form["xfd"]);
                string tese = Request.Form["tese"];
                string zhekou = Request.Form["zhekou"];
                decimal fl = decimal.Parse(Request.Form["fl"]);
                string lxfs = Request.Form["lxfs"];
                int yjrmb = Int32.Parse(Request.Form["yjrmb"]);
                int payrmb = Int32.Parse(Request.Form["payrmb"]);
                string khh = Request.Form["khh"];
                string khm = Request.Form["khm"];
                string kahao = Request.Form["kahao"];
                string qx = Request.Form["qx"];
                Guid flgid = Guid.Parse(Request.Form["flgid"]);
                if (db.ShopList.Where(l => l.name == name && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("名称已存在！", "history.go(-1);", "商家名称已存在");
                }
                else
                {
                    if (gid == null)
                    {
                        b = new ShopList();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                        b.usergid = Guid.NewGuid();
                        b.account = account;
                        b.loginid = "0000000000";
                        b.grade = "";
                        b.rmb = 0;
                    }
                    else
                    {
                        b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    if (!string.IsNullOrEmpty(picture))
                    {
                        b.picture = picture;
                    }
                    b.flgid = flgid;
                    b.name = name;
                    b.explain = explain;
                    b.introduce = introduce;
                    b.sort = sort;
                    if (b.pwd != pwd)
                    {
                        b.pwd = Common.MD5.GetMD5ljsheng(pwd);
                    }
                    b.province = province;
                    b.city = city;
                    b.area = area;
                    if (!string.IsNullOrEmpty(picture))
                    {
                        b.picture = picture;
                    }
                    b.address = address;
                    b.openid = openid;
                    b.qx = qx;
                    if (Request.Form["lx"] != "3")
                    {
                        b.grade = grade;
                        b.show = show == "显示" ? 1 : 2;
                    }
                    else
                    {
                        b.show = show == "显示" ? 1 : 2;
                    }
                    b.xfx = xfx;
                    b.xfd = xfd;
                    b.tese = tese;
                    b.zhekou = zhekou;
                    b.fl = fl;
                    b.lxfs = lxfs;
                    b.yjrmb = yjrmb;
                    b.payrmb = payrmb;
                    b.khh = khh;
                    b.khm = khm;
                    b.kahao = kahao;
                    if (gid == null)
                    {
                        db.ShopList.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        return Helper.WebRedirect("操作成功！", "/ljsheng/cplist", "操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败,请检查录入的数据！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }
        }

        // 列表管理
        public ActionResult cplist()
        {
            using (EFDB db = new EFDB())
            {
                string province = LCookie.Getgly("province");
                string city = LCookie.Getgly("city");
                string area = LCookie.Getgly("area");
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string pstime = ViewBag.pstime = Request.Form["pstime"];
                string petime = ViewBag.petime = Request.Form["petime"];
                string name = ViewBag.name = Request.Form["name"];
                ViewBag.cpfl = db.CPFL.ToList();
                Guid? flgid = null;
                if (!string.IsNullOrEmpty(Request.QueryString["flgid"]) && Request.QueryString["flgid"] != "0")
                {
                    flgid = ViewBag.flgid = Guid.Parse(Request.QueryString["flgid"]);
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request.Form["flgid"]) && Request.Form["flgid"] != "0")
                    {
                        flgid = ViewBag.flgid = Guid.Parse(Request.Form["flgid"]);
                    }
                }
                var b = db.ShopList.AsQueryable();
                if (!string.IsNullOrEmpty(province))
                {
                    b = b.Where(l => l.province.Contains(province));
                }
                if (!string.IsNullOrEmpty(city))
                {
                    b = b.Where(l => l.city.Contains(city));
                }
                if (!string.IsNullOrEmpty(area))
                {
                    b = b.Where(l => l.area.Contains(area));
                }
                //不查询注册套餐
                if (Request.QueryString["lx"] == "3")
                {
                    b = b.Where(l => l.grade == "注册套餐");
                }
                else
                {
                    b = b.Where(l => l.grade != "注册套餐");
                }
                //产品分类
                if (flgid != null)
                {
                    b = b.Where(l => l.flgid == flgid);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                //收入时间查询
                DateTime? pst = DateTime.Parse("1970-01-01 01:01:01");
                DateTime? pet = DateTime.Now;
                if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                {
                    if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        pst = DateTime.Parse(pstime);
                        pet = DateTime.Parse(petime);
                    }
                }
                decimal parame1 = 0;
                if (b.Count() > 0)
                {
                    parame1 = ViewBag.parame1 = b.GroupJoin(db.Pay,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        rmb = db.Pay.Where(p => p.usergid == x.usergid && p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.usergid && p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                    }).Sum(l => l.rmb);
                }
                //decimal parame1 = ViewBag.parame1 = db.Pay.Where(p => p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.lx == 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0;
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                if (Request.Form["daochu"] == "daochu")
                {

                    {
                        var list = b.ToList();
                        //创建Excel文件的对象
                        NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                        //添加一个sheet
                        NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                        //给sheet1添加第一行的头部标题
                        NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                        row1.CreateCell(0).SetCellValue("商家名称");
                        row1.CreateCell(1).SetCellValue("商家帐号");
                        row1.CreateCell(2).SetCellValue("商家等级");
                        row1.CreateCell(3).SetCellValue("收入");
                        row1.CreateCell(4).SetCellValue("注册时间");
                        //将数据逐步写入sheet1各个行
                        int i = 0;
                        for (i = 0; i < list.Count; i++)
                        {
                            NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                            rowtemp.CreateCell(0).SetCellValue(list[i].name);
                            rowtemp.CreateCell(1).SetCellValue(list[i].account);
                            rowtemp.CreateCell(2).SetCellValue(list[i].grade);
                            rowtemp.CreateCell(3).SetCellValue(Helper.GetShopRMB(list[i].usergid, pstime, petime));
                            rowtemp.CreateCell(4).SetCellValue(list[i].addtime.ToString());
                        }
                        NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                        //统计
                        rowend.CreateCell(1).SetCellValue("当前查询条件商家营业额=" + parame1);
                        // 写入到客户端 
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
                        book.Write(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        return File(ms, "application/vnd.ms-excel", "商家.xls");
                    }
                }
                else
                {
                    return View(b.OrderBy(l => l.sort).OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult cpdelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.ShopList.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        /// <summary>
        /// 客满
        /// </summary>
        [HttpPost]
        public JsonResult cpkm(Guid gid, int sort)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.ShopList.Where(l => l.gid == gid).Update(l => new ShopList { sort = sort == 0 ? 1 : 0 }) == 1)
                    {
                        return Json(new AjaxResult(sort == 0 ? 1 : 0));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        /// <summary>
        /// 重设密码
        /// </summary>
        [HttpPost]
        public JsonResult shmima(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    string pwd = RandStr.CreateValidateNumber(6);
                    var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    b.pwd = MD5.GetMD5ljsheng(pwd);
                    if (db.SaveChanges() == 1)
                    {
                        db.LUser.Where(l => l.gid == b.usergid).Update(l => new LUser { pwd = b.pwd });
                        return Json(new AjaxResult("新密码为:" + pwd));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 商家折扣模块
        /// <summary>
        /// 增加折扣
        /// </summary>
        [HttpPost]
        public JsonResult zhekouau(Guid shopgid, string name, decimal mingzhe, decimal anzhe)
        {
            if (shopgid != null)
            {
                using (EFDB db = new EFDB())
                {
                    var b = new zhekou();
                    b.gid = Guid.NewGuid();
                    b.addtime = DateTime.Now;
                    b.shopgid = shopgid;
                    b.name = name;
                    b.mingzhe = mingzhe;
                    b.anzhe = anzhe;
                    db.zhekou.Add(b);
                    if (db.SaveChanges() == 1)
                    {
                        return Json(new AjaxResult("增加折扣类型成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "增加折扣类型失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        // 列表管理
        public ActionResult zhekou()
        {
            using (EFDB db = new EFDB())
            {
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string name = ViewBag.name = Request.Form["name"];
                Guid shopgid = Guid.Parse(Request.QueryString["gid"]);
                var b = db.zhekou.Where(l => l.shopgid == shopgid).AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }

                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                return View(b.OrderBy(l => l.addtime).OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult zkdelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.zhekou.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 图片上传
        /// <summary>
        /// 图片上传
        /// </summary>
        [HttpPost]
        public JsonResult upload(string path, HttpPostedFileBase picture = null)
        {
            string filename = "";
            if (picture != null)
            {
                filename = Helper.UploadFiles(path, picture);
            }
            return Json(new AjaxResult(filename));
        }

        /// <summary>
        /// 编辑器的文件上传
        /// </summary>
        [HttpPost]
        public JsonResult bjqupload()
        {
            HttpPostedFileBase picture = Request.Files["file"];
            string filename = LJShengHelper.BJQ;
            if (picture != null)
            {
                filename += Helper.UploadFiles(LJShengHelper.BJQ, picture);
            }
            return Json(new { code = 0, msg = "", data = new { src = filename, title = "" } });
        }
        #endregion

        #region 城市管理
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult opencity()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.OpenCity.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.sort = b.sort;
                    ViewBag.show = b.show;
                }
            }
            else
            {
                ViewBag.sort = 1;
            }
            return View();
        }
        [HttpPost]
        public ActionResult opencity(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                OpenCity b;
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                int sort = Int32.Parse(Request.Form["sort"]);
                string show = Request.Form["show"];
                if (db.OpenCity.Where(l => l.city == city && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("已存在！", "history.go(-1);", "已存在");
                }
                else
                {
                    if (gid == null)
                    {
                        b = new OpenCity();
                        b.gid = Guid.NewGuid();
                        b.addtime = DateTime.Now;
                    }
                    else
                    {
                        b = db.OpenCity.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    b.province = province;
                    b.city = city;
                    b.sort = sort;
                    b.show = show == "显示" ? 1 : 2;
                    if (gid == null)
                    {
                        db.OpenCity.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        return Helper.WebRedirect("操作成功！", "history.go(-1);", "操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败,请检查录入的数据！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }
        }

        /// <summary>
        /// 开通城市
        /// </summary>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public ActionResult citylist()
        {
            using (EFDB db = new EFDB())
            {
                string stime = ViewBag.stime = Request.Form["stime"];
                string etime = ViewBag.etime = Request.Form["etime"];
                string city = ViewBag.name = Request.Form["city"];
                var b = db.OpenCity.AsQueryable();
                if (!string.IsNullOrEmpty(city))
                {
                    b = b.Where(l => l.city.Contains(city));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = 1;
                if (!string.IsNullOrEmpty(Request.Form["pageindex"]))
                {
                    pageindex = Int32.Parse(Request.Form["pageindex"]);
                }
                int pagesize = 10;//每页显示的数量
                ViewBag.pageindex = pageindex;//当前页数
                ViewBag.count = b.Count();//总条数
                ViewBag.pages = Math.Ceiling((double)ViewBag.count / (double)pagesize);//总页数
                return View(b.OrderBy(l => l.sort).OrderBy(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList());
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult citydelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.OpenCity.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        /// <summary>
        /// 选择地域
        /// </summary>
        public ActionResult city()
        {
            ViewBag.province = Common.LCookie.Getgly("province");
            ViewBag.city = Common.LCookie.Getgly("city");
            ViewBag.area = Common.LCookie.Getgly("area");
            return View();
        }
        [HttpPost]
        public ActionResult cityck()
        {
            string province = Request.Form["province"];
            string city = Request.Form["city"];
            string area = Request.Form["area"];
            Common.LCookie.AddCookie("gly", DESRSA.DESEnljsheng(JsonConvert.SerializeObject(new
            {
                province = province == "请选择省份" ? "" : province,
                city = city == "请选择城市" ? "" : city,
                area = area == "请选择区域" ? "" : area
            })), 0);
            return new RedirectResult("/ljsheng/hylist");
        }
        #endregion

        #region 订单管理
        /// <summary>
        /// 调整天数
        /// </summary>
        /// <returns></returns>
        public ActionResult tianlist()
        {
            return View();
        }
        /// <summary>
        /// 调整天数
        /// </summary>
        [HttpPost]
        public JsonResult AT(int tian)
        {
            if (tian >0)
            {
                int all = 0;
                using (EFDB db = new EFDB())
                {
                    db.LOrder.Update(l => new LOrder { tgf = 0 });
                    var b = db.LOrder.Where(l => l.pay_status == 1 && l.remarks != "").ToList();
                    if (b.Count > 0)
                    {
                        string[] s;
                        foreach (var dr in b)
                        {
                            try
                            {
                                s = dr.remarks.Split(' ');
                                if (s.Length == 4)
                                {
                                    string beizhu = s[0] + " " + s[1] + " " + s[2] + " |" + DateTime.Parse(s[3].Replace("|", "").Replace(".", "-")).AddDays(tian).ToString("yyyy.MM.dd") + "|";
                                    all += db.LOrder.Where(l => l.gid == dr.gid).Update(l => new LOrder { remarks = beizhu, tgf = tian });
                                }
                            }
                            catch { }
                        }
                        return Json(new AjaxResult("调整天数机器=" + all.ToString()));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "没有符合的数据"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }


        [HttpPost]
        public ActionResult aatlist()
        {
            using (EFDB db = new EFDB())
            {
                var b = db.LOrder.Select(l => new
                {
                    l.gid,
                    l.addtime,
                    l.usergid,
                    l.buygid,
                    l.order_no,
                    l.trade_no,
                    l.pay_type,
                    l.pay_time,
                    l.pay_status,
                    l.ptf,
                    l.hyfwf,
                    l.tgf,
                    l.amount,
                    l.rmb,
                    l.user_cash,
                    l.remarks,
                    l.coupon_number,
                    l.zhekou,
                    l.num,
                    l.tianshu,
                    db.ShopList.Where(s => s.gid == l.buygid).FirstOrDefault().name
                }).AsQueryable();
                b = b.Where(l => l.pay_status == 1 && l.remarks!="");
                //开始分页
                int pageindex = 1;
                int pagesize = 888888;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }

        public ActionResult ddlist()
        {
            if (Request.Form["daochu"] == "daochu")
            {
                using (EFDB db = new EFDB())
                {
                    string stime = Request.Form["stime"];
                    string etime = Request.Form["etime"];
                    string order_no = Request.Form["order_no"];
                    string account = Request.Form["account"];
                    string name = Request.Form["name"];
                    int pay_type = string.IsNullOrEmpty(Request.Form["pay_type"]) ? 0 : Int32.Parse(Request.Form["pay_type"]);
                    int pay_status = string.IsNullOrEmpty(Request.Form["pay_status"]) ? 0 : Int32.Parse(Request.Form["pay_status"]);
                    Guid? usergid = null;
                    if (!string.IsNullOrEmpty(Request.Form["usergid"]))
                    {
                        usergid = Guid.Parse(Request.Form["usergid"]);
                    }
                    Guid? buygid = null;
                    if (!string.IsNullOrEmpty(Request.Form["buygid"]))
                    {
                        buygid = Guid.Parse(Request.Form["buygid"]);
                    }
                    var b = db.LOrder.Select(l => new
                    {
                        l.gid,
                        l.addtime,
                        l.usergid,
                        l.buygid,
                        l.order_no,
                        l.trade_no,
                        l.pay_type,
                        l.pay_time,
                        l.pay_status,
                        l.ptf,
                        l.hyfwf,
                        l.tgf,
                        l.amount,
                        l.rmb,
                        l.user_cash,
                        l.remarks,
                        l.coupon_number,
                        l.zhekou,
                        db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                        username = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name,
                        db.ShopList.Where(s => s.gid == l.buygid).FirstOrDefault().name
                    }).AsQueryable();
                    if (usergid != null)
                    {
                        b = b.Where(l => l.usergid == usergid);
                    }
                    if (buygid != null)
                    {
                        b = b.Where(l => l.buygid == buygid);
                    }
                    if (!string.IsNullOrEmpty(order_no))
                    {
                        b = b.Where(l => (l.order_no.Contains(order_no) || l.trade_no.Contains(order_no)));
                    }
                    if (pay_type != 0)
                    {
                        b = b.Where(l => l.pay_type == pay_type);
                    }
                    if (pay_status != 0)
                    {
                        b = b.Where(l => l.pay_status == pay_status);
                    }
                    if (!string.IsNullOrEmpty(account))
                    {
                        b = b.Where(l => l.account.Contains(account));
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        b = b.Where(l => l.name.Contains(name));
                    }
                    //时间查询
                    if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                    {
                        DateTime? st = null;
                        DateTime? et = null;
                        if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            st = DateTime.Parse(stime);
                            et = DateTime.Parse(etime);
                        }
                        b = b.Where(l => l.addtime >= st && l.addtime <= et);
                    }
                    //获取list数据
                    var list = b.OrderByDescending(l => l.addtime).ToList();

                    //创建Excel文件的对象
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //添加一个sheet
                    NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                    //给sheet1添加第一行的头部标题
                    NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                    row1.CreateCell(0).SetCellValue("系统订单号");
                    row1.CreateCell(1).SetCellValue("网上银行订单");
                    row1.CreateCell(2).SetCellValue("会员帐号");
                    row1.CreateCell(3).SetCellValue("商家名称");
                    row1.CreateCell(4).SetCellValue("支付状态");
                    row1.CreateCell(5).SetCellValue("平台/会员/推广费");
                    row1.CreateCell(6).SetCellValue("原价");
                    row1.CreateCell(7).SetCellValue("支付金额");
                    //将数据逐步写入sheet1各个行
                    int i = 0;
                    for (i = 0; i < list.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                        rowtemp.CreateCell(0).SetCellValue(list[i].order_no);
                        rowtemp.CreateCell(1).SetCellValue(list[i].trade_no);
                        rowtemp.CreateCell(2).SetCellValue(list[i].account + "(" + list[i].username + ")");
                        rowtemp.CreateCell(3).SetCellValue(list[i].name);
                        rowtemp.CreateCell(4).SetCellValue(list[i].pay_status == 1 ? "已付" : "未付");
                        rowtemp.CreateCell(5).SetCellValue(list[i].ptf.ToString() + "/" + list[i].hyfwf.ToString() + "/" + list[i].tgf.ToString() + "/");
                        rowtemp.CreateCell(6).SetCellValue(list[i].amount.ToString());
                        rowtemp.CreateCell(7).SetCellValue(list[i].rmb.ToString());
                    }
                    NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                    //统计
                    decimal parame1 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                    rowend.CreateCell(1).SetCellValue("当前查询条件总收入=" + parame1);
                    // 写入到客户端 
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    book.Write(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms, "application/vnd.ms-excel", "订单.xls");
                }
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult aorderlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string order_no = paramJson["order_no"].ToString();
                string account = paramJson["account"].ToString();
                string name = paramJson["name"].ToString();
                string kami = paramJson["kami"].ToString();
                string post = paramJson["post"].ToString();
                int pay_type = string.IsNullOrEmpty(paramJson["pay_type"].ToString()) ? 0 : Int32.Parse(paramJson["pay_type"].ToString());
                int pay_status = string.IsNullOrEmpty(paramJson["pay_status"].ToString()) ? 0 : Int32.Parse(paramJson["pay_status"].ToString());
                Guid? usergid = null;
                if (!string.IsNullOrEmpty(paramJson["usergid"].ToString()))
                {
                    usergid = Guid.Parse(paramJson["usergid"].ToString());
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["usergid"]))
                    {
                        usergid = Guid.Parse(Request.QueryString["usergid"]);
                    }
                }
                Guid? buygid = null;
                if (!string.IsNullOrEmpty(paramJson["buygid"].ToString()))
                {
                    buygid = Guid.Parse(paramJson["buygid"].ToString());
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["buygid"]))
                    {
                        buygid = Guid.Parse(Request.QueryString["buygid"]);
                    }
                }
                var b = db.LOrder.Select(l => new
                {
                    l.gid,
                    l.addtime,
                    l.usergid,
                    l.buygid,
                    l.order_no,
                    l.trade_no,
                    l.pay_type,
                    l.pay_time,
                    l.pay_status,
                    l.ptf,
                    l.hyfwf,
                    l.tgf,
                    l.amount,
                    l.rmb,
                    l.user_cash,
                    l.remarks,
                    l.coupon_number,
                    l.zhekou,
                    l.num,
                    l.tianshu,
                    db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                    username = db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().real_name,
                    db.ShopList.Where(s => s.gid == l.buygid).FirstOrDefault().name
                }).AsQueryable();
                if (usergid != null)
                {
                    b = b.Where(l => l.usergid == usergid);
                }
                if (buygid != null)
                {
                    b = b.Where(l => l.buygid == buygid);
                }
                if (!string.IsNullOrEmpty(order_no))
                {
                    b = b.Where(l => (l.order_no.Contains(order_no) || l.trade_no.Contains(order_no)));
                }
                if (pay_type != 0)
                {
                    b = b.Where(l => l.pay_type == pay_type);
                }
                if (pay_status != 0)
                {
                    b = b.Where(l => l.pay_status == pay_status);
                }
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                if (!string.IsNullOrEmpty(kami))
                {
                    b = b.Where(l => l.coupon_number.Contains(kami));
                }
                if (!string.IsNullOrEmpty(post))
                {
                    b = b.Where(l => l.remarks.Contains(post));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.pay_time >= st && l.pay_time <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                decimal parame1 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult orderdelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.LOrder.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        /// <summary>
        /// 删除过期机器码
        /// </summary>
        [HttpPost]
        public JsonResult orderdeletejqm(string gid)
        {
            if (gid == "9021616")
            {
                int sl = 0;
                using (EFDB db = new EFDB())
                {
                    var jqzc = db.LOrder.Where(l => l.pay_status == 1);
                    if (jqzc.Count() > 0)
                    {
                        foreach (var d in jqzc)
                        {
                            if (!string.IsNullOrEmpty(d.remarks) && !d.remarks.Contains("已续费机器"))
                            {
                                try
                                {
                                    string[] jqxf = d.remarks.Split(' ');
                                    if (DateTime.Parse(jqxf[3].Replace("|", "").Replace(".", "-")) < DateTime.Now)
                                    {
                                        using (EFDB dbjqm = new EFDB())
                                        {
                                            if (dbjqm.LOrder.Where(l => l.gid == d.gid).Delete() == 1)
                                            {
                                                sl++;
                                            }
                                        }
                                    }
                                }
                                catch { }
                            }
                        }
                        return Json(new AjaxResult("成功删除机器码(" + sl.ToString() + ")条记录,重新进入页面更新最新数据!"));
                    }
                }
                return Json(new AjaxResult("没有过期的机器"));
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 提现管理
        public ActionResult paylist()
        {
            if (Request.Form["daochu"] == "daochu")
            {
                using (EFDB db = new EFDB())
                {
                    string stime = Request.Form["stime"];
                    string etime = Request.Form["etime"];
                    string pstime = Request.Form["pstime"];
                    string petime = Request.Form["petime"];
                    string account = Request.Form["account"];
                    string name = Request.Form["name"];
                    int pay_type = string.IsNullOrEmpty(Request.Form["pay_type"]) ? 0 : Int32.Parse(Request.Form["pay_type"]);
                    int pay_status = string.IsNullOrEmpty(Request.Form["pay_status"]) ? 0 : Int32.Parse(Request.Form["pay_status"]);
                    int lx = string.IsNullOrEmpty(Request.Form["lx"]) ? 100 : Int32.Parse(Request.Form["lx"]);
                    Guid? usergid = null;
                    if (!string.IsNullOrEmpty(Request.Form["usergid"]))
                    {
                        usergid = Guid.Parse(Request.Form["usergid"]);
                    }
                    Guid? ddgid = null;
                    if (!string.IsNullOrEmpty(Request.Form["ddgid"]))
                    {
                        ddgid = Guid.Parse(Request.Form["ddgid"]);
                    }
                    var b = db.Pay.GroupJoin(db.LUser,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        x.gid,
                        x.addtime,
                        x.usergid,
                        x.ddgid,
                        x.pay_type,
                        x.pay_time,
                        x.pay_status,
                        x.rmb,
                        x.lx,
                        x.remarks,
                        x.gly,
                        y.FirstOrDefault().account,
                        y.FirstOrDefault().real_name,
                        y.FirstOrDefault().ewm,
                        y.FirstOrDefault().khh,
                        y.FirstOrDefault().khm,
                        y.FirstOrDefault().kahao,
                        db.ShopList.Where(s => s.gid == db.LOrder.Where(o => o.gid == x.ddgid).FirstOrDefault().buygid).FirstOrDefault().name
                    }).AsQueryable();
                    if (usergid != null)
                    {
                        b = b.Where(l => l.usergid == usergid);
                    }
                    if (ddgid != null)
                    {
                        b = b.Where(l => l.ddgid == ddgid);
                    }
                    if (pay_type != 0)
                    {
                        b = b.Where(l => l.pay_type == pay_type);
                    }
                    if (pay_status != 0)
                    {
                        b = b.Where(l => l.pay_status == pay_status);
                    }
                    if (lx != 100)
                    {
                        b = b.Where(l => l.lx == lx);
                    }
                    if (!string.IsNullOrEmpty(account))
                    {
                        b = b.Where(l => l.account.Contains(account));
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        b = b.Where(l => l.name.Contains(name));
                    }
                    //时间查询
                    if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                    {
                        DateTime? st = null;
                        DateTime? et = null;
                        if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                        {
                            st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                            et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            st = DateTime.Parse(stime);
                            et = DateTime.Parse(etime);
                        }
                        b = b.Where(l => l.addtime >= st && l.addtime <= et);
                    }
                    //收入时间查询
                    if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                    {
                        DateTime? pst = null;
                        DateTime? pet = null;
                        if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                        {
                            pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                            pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                        }
                        else
                        {
                            pst = DateTime.Parse(pstime);
                            pet = DateTime.Parse(petime);
                        }
                        b = b.Where(l => l.pay_time >= pst && l.pay_time <= pet);
                    }
                    //获取list数据
                    var list = b.OrderByDescending(l => l.addtime).OrderBy(l => l.addtime).ToList();

                    //创建Excel文件的对象
                    NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                    //添加一个sheet
                    NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");

                    //给sheet1添加第一行的头部标题
                    NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
                    row1.CreateCell(0).SetCellValue("提现人");
                    row1.CreateCell(1).SetCellValue("金额");
                    row1.CreateCell(2).SetCellValue("提现类型");
                    row1.CreateCell(3).SetCellValue("申请时间");
                    row1.CreateCell(4).SetCellValue("提现时间");
                    row1.CreateCell(5).SetCellValue("操作人");
                    row1.CreateCell(6).SetCellValue("状态");
                    row1.CreateCell(7).SetCellValue("商家");
                    //将数据逐步写入sheet1各个行
                    int i = 0;
                    for (i = 0; i < list.Count; i++)
                    {
                        NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                        Guid gid = list[i].usergid;
                        string shopname = lx == 3 ? db.ShopList.Where(l => l.usergid == gid).FirstOrDefault().name : "";
                        rowtemp.CreateCell(0).SetCellValue(list[i].account + "(" + list[i].real_name + ")" + shopname);
                        rowtemp.CreateCell(1).SetCellValue(list[i].rmb.ToString());
                        rowtemp.CreateCell(2).SetCellValue(list[i].pay_type == 2 ? "微信" : "线下付款");
                        rowtemp.CreateCell(3).SetCellValue(list[i].addtime.ToString());
                        rowtemp.CreateCell(4).SetCellValue(list[i].pay_time == null ? "" : list[i].pay_time.ToString());
                        rowtemp.CreateCell(5).SetCellValue(list[i].gly);
                        rowtemp.CreateCell(6).SetCellValue(list[i].pay_status == 1 ? "已付" : "未付");
                        rowtemp.CreateCell(7).SetCellValue(list[i].name);
                    }
                    NPOI.SS.UserModel.IRow rowend = sheet1.CreateRow(i + 1);
                    //统计
                    decimal parame1 = b.Count() > 0 ? b.Sum(l => l.rmb) : 0;
                    decimal parame2 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                    decimal parame3 = b.Where(l => l.pay_status == 2).Count() > 0 ? b.Where(l => l.pay_status == 2).Sum(l => l.rmb) : 0;
                    rowend.CreateCell(1).SetCellValue("当前查询条件总提现=" + parame1 + "(已提现=" + parame2 + ")(未提现=" + parame3);
                    // 写入到客户端 
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    book.Write(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    return File(ms, "application/vnd.ms-excel", "提现.xls");
                }
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult apaylist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string pstime = paramJson["pstime"].ToString();
                string petime = paramJson["petime"].ToString();
                string account = paramJson["account"].ToString();
                string name = paramJson["name"].ToString();
                int pay_type = string.IsNullOrEmpty(paramJson["pay_type"].ToString()) ? 0 : Int32.Parse(paramJson["pay_type"].ToString());
                int pay_status = string.IsNullOrEmpty(paramJson["pay_status"].ToString()) ? 0 : Int32.Parse(paramJson["pay_status"].ToString());
                int lx = string.IsNullOrEmpty(paramJson["lx"].ToString()) ? 100 : Int32.Parse(paramJson["lx"].ToString());
                //会员
                Guid? usergid = null;
                if (!string.IsNullOrEmpty(paramJson["usergid"].ToString()))
                {
                    usergid = Guid.Parse(paramJson["usergid"].ToString());
                }
                Guid? ddgid = null;
                if (!string.IsNullOrEmpty(paramJson["ddgid"].ToString()))
                {
                    ddgid = Guid.Parse(paramJson["ddgid"].ToString());
                }
                var b = db.Pay.GroupJoin(db.LUser,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        x.gid,
                        x.addtime,
                        x.usergid,
                        x.ddgid,
                        x.pay_type,
                        x.pay_time,
                        x.pay_status,
                        x.rmb,
                        x.lx,
                        x.remarks,
                        x.gly,
                        y.FirstOrDefault().account,
                        y.FirstOrDefault().real_name,
                        y.FirstOrDefault().openid,
                        y.FirstOrDefault().qx,
                        y.FirstOrDefault().ewm,
                        y.FirstOrDefault().khh,
                        y.FirstOrDefault().khm,
                        y.FirstOrDefault().kahao,
                        db.ShopList.Where(s => s.gid == db.LOrder.Where(o => o.gid == x.ddgid).FirstOrDefault().buygid).FirstOrDefault().name
                    }).AsQueryable();
                if (usergid != null)
                {
                    b = b.Where(l => l.usergid == usergid);
                }
                if (ddgid != null)
                {
                    b = b.Where(l => l.ddgid == ddgid);
                }
                if (pay_type != 0)
                {
                    b = b.Where(l => l.pay_type == pay_type);
                }
                if (pay_status != 0)
                {
                    b = b.Where(l => l.pay_status == pay_status);
                }
                if (lx != 100)
                {
                    b = b.Where(l => l.lx == lx);
                }
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //收入时间查询
                if (!string.IsNullOrEmpty(pstime) || !string.IsNullOrEmpty(petime))
                {
                    DateTime? pst = null;
                    DateTime? pet = null;
                    if (!string.IsNullOrEmpty(pstime) && string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(pstime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(pstime) && !string.IsNullOrEmpty(petime))
                    {
                        pst = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 00:00:00");
                        pet = DateTime.Parse(DateTime.Parse(petime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        pst = DateTime.Parse(pstime);
                        pet = DateTime.Parse(petime);
                    }
                    b = b.Where(l => l.pay_time >= pst && l.pay_time <= pet);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                decimal parame1 = count > 0 ? b.Sum(l => l.rmb) : 0;
                decimal parame2 = b.Where(l => l.pay_status == 1).Count() > 0 ? b.Where(l => l.pay_status == 1).Sum(l => l.rmb) : 0;
                decimal parame3 = b.Where(l => l.pay_status == 2).Count() > 0 ? b.Where(l => l.pay_status == 2).Sum(l => l.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    parame2,
                    parame3,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderBy(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }

        /// <summary>
        /// 更新订单状态
        /// </summary>
        [HttpPost]
        public JsonResult upfk(Guid gid, int pay_type)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    string gly = LCookie.Getljsheng("account");
                    if (db.Pay.Where(l => l.gid == gid).Update(l => new Pay { pay_type = pay_type, pay_status = 1, pay_time = DateTime.Now, gly = gly }) == 1)
                    {
                        Common.LogManager.WriteLog("提现成功", "gid=" + gid + ",操作员:" + gly);
                        return Json(new AjaxResult("更新用户提现成功"));
                    }
                    else
                    {
                        Common.LogManager.WriteLog("提现失败", "gid=" + gid + ",操作员:" + gly);
                        return Json(new AjaxResult(300, "更新用户提现失败,请记录界面显示的操作代码联系技术人员!"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }

        /// <summary>
        /// 微信自动打款
        /// </summary>
        [HttpPost]
        public JsonResult wxfk(Guid gid, string real_name, string openid)
        {
            using (EFDB db = new EFDB())
            {
                var b = db.Pay.Where(l => l.gid == gid).FirstOrDefault();
                if (b != null)
                {
                    //string amount = (b.rmb * 100).ToString("f0");
                    //string partner_trade_no = RandStr.CreateOrderNO();
                    //string RequestXML = b.request_xml;
                    //if (b.remarks != "SYSTEMERROR")
                    //{
                    //    string wx_nonceStr = LJSheng.WX.WXPay.getNoncestr();
                    //    //设置签名
                    //    SortedDictionary<string, string> dic = new SortedDictionary<string, string>();
                    //    dic.Add("mch_appid", LJShengHelper.appid);
                    //    dic.Add("mchid", LJShengHelper.mch_id);
                    //    dic.Add("nonce_str", wx_nonceStr);
                    //    dic.Add("partner_trade_no", partner_trade_no);
                    //    dic.Add("openid", openid);
                    //    //dic.Add("check_name", "NO_CHECK");
                    //    dic.Add("check_name", "FORCE_CHECK");
                    //    dic.Add("re_user_name", real_name);
                    //    dic.Add("amount", amount);
                    //    dic.Add("desc", "最惠联盟");
                    //    dic.Add("spbill_create_ip", LJShengHelper.IP);
                    //    string get_sign = LJSheng.WX.WXPay.BuildRequest(dic, LJShengHelper.api_key);
                    //    //请求参数
                    //    StringBuilder XML = new StringBuilder();
                    //    XML.Append("<xml>");
                    //    XML.Append("<mch_appid>" + LJShengHelper.appid + "</mch_appid>");
                    //    XML.Append("<mchid>" + LJShengHelper.mch_id + "</mchid>");
                    //    XML.Append("<nonce_str>" + wx_nonceStr + "</nonce_str>");
                    //    XML.Append("<partner_trade_no>" + partner_trade_no + "</partner_trade_no>");
                    //    XML.Append("<openid>" + openid + "</openid>");
                    //    //XML.Append("<check_name>NO_CHECK</check_name>");
                    //    XML.Append("<check_name>FORCE_CHECK</check_name>");
                    //    XML.Append("<re_user_name>" + real_name + "</re_user_name>");
                    //    XML.Append("<amount>" + amount + "</amount>");
                    //    XML.Append("<desc>最惠联盟</desc>");
                    //    XML.Append("<spbill_create_ip>" + LJShengHelper.IP + "</spbill_create_ip>");
                    //    XML.Append("<sign>" + get_sign + "</sign>");
                    //    XML.Append("</xml>");
                    //    RequestXML = XML.ToString();
                    //}
                    //System.Net.WebClient wc = new System.Net.WebClient();
                    //byte[] bs = System.Text.Encoding.UTF8.GetBytes(RequestXML);
                    //byte[] bsReturn = wc.UploadData("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", bs);
                    //string strReturn = System.Text.Encoding.UTF8.GetString(bsReturn);
                    string partner_trade_no = RandStr.CreateOrderNO();
                    if (!string.IsNullOrEmpty(b.order_no))
                    {
                        partner_trade_no = b.order_no;
                    }
                    string[] strReturn = "sd ad a".Split(' ');
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.LoadXml(strReturn[0]);
                    string return_code = xmldoc.SelectSingleNode("/xml/return_code").InnerText;
                    string result_code = xmldoc.SelectSingleNode("/xml/result_code").InnerText;
                    string return_msg = xmldoc.SelectSingleNode("/xml/return_msg").InnerText;
                    string gly = LCookie.Getljsheng("account");
                    b.gly = gly;
                    b.request_xml = strReturn[1];
                    if (return_code == "SUCCESS" && result_code == "SUCCESS")//验证成功
                    {
                        //partner_trade_no = xmldoc.SelectSingleNode("/xml/partner_trade_no").InnerText;//系统订单号
                        string payment_no = xmldoc.SelectSingleNode("/xml/payment_no").InnerText;//微信订单号
                        string payment_time = xmldoc.SelectSingleNode("/xml/payment_time").InnerText;//微信付款时间
                        b.pay_type = 2;
                        b.pay_status = 1;
                        b.pay_time = DateTime.Parse(payment_time);
                        b.order_no = partner_trade_no;
                        b.payment_no = payment_no;
                        b.remarks = strReturn[0];
                    }
                    else
                    {
                        return_msg = xmldoc.SelectSingleNode("/xml/err_code_des").InnerText;
                        string err_code = xmldoc.SelectSingleNode("/xml/err_code").InnerText;
                        if (err_code == "SYSTEMERROR")
                        {
                            b.remarks = "SYSTEMERROR";
                        }
                        else
                        {
                            b.order_no = "";
                            b.remarks = "";
                        }
                    }
                    string msg = "gid=" + gid + ",操作员:" + gly + "return_code=" + return_code + ",result_code:" + result_code + ",return_msg:" + return_msg;
                    if (db.SaveChanges() == 1)
                    {
                        Common.LogManager.WriteLog("自动提现更新数据成功", msg);
                        if (return_code == "SUCCESS" && result_code == "SUCCESS")//验证成功
                        {
                            return Json(new AjaxResult("自动提现成功"));
                        }
                        else
                        {
                            return Json(new AjaxResult(300, return_msg));
                        }
                    }
                    else
                    {
                        if (return_code == "SUCCESS" && result_code == "SUCCESS")//验证成功
                        {
                            Common.LogManager.WriteLog("自动提现更新数据失败提现成功", msg);
                            return Json(new AjaxResult("自动提现成功更新数据失败"));
                        }
                        else
                        {
                            return Json(new AjaxResult(300, return_msg));
                        }
                    }
                }
                else
                {
                    return Json(new AjaxResult(300, "非法参数!"));
                }
            }
        }
        #endregion

        #region 短信管理
        /// <summary>
        /// 短信管理
        /// </summary>
        /// <param name="ljsheng">传入参数</param>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public ActionResult duanxin(string ljsheng)
        {
            return View();
        }
        [HttpPost]
        public ActionResult aduanxinlist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine());
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string account = paramJson["account"].ToString();
                var b = db.SMS.AsQueryable();
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.shouji.Contains(account));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion

        #region 评论管理
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult plimg()
        {
            return View();
        }
        [HttpPost]
        public ActionResult aplimglist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            Guid plgid = Guid.Parse(paramJson["gid"].ToString());
            int lx = Int32.Parse(paramJson["lx"].ToString());
            using (EFDB db = new EFDB())
            {
                var b = db.pllist.Where(l => l.plgid == plgid && l.lx == lx).AsQueryable();
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult plau()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
            {
                using (EFDB db = new EFDB())
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.PL.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.msg = b.msg;
                    ViewBag.tomsg = b.tomsg;
                    ViewBag.shoppj = b.shoppj;
                    ViewBag.userpj = b.userpj;
                    ViewBag.show = b.show;
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult plau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                string msg = Request.Form["msg"];
                string tomsg = Request.Form["tomsg"];
                string show = Request.Form["show"];
                string shoppj = Request.Form["shoppj"];
                string userpj = Request.Form["userpj"];
                var b = db.PL.Where(l => l.gid == gid).FirstOrDefault();
                b.msg = msg;
                b.tomsg = string.IsNullOrEmpty(tomsg) ? "等待回复" : tomsg;
                b.shoppj = shoppj == "优" ? 1 : shoppj == "差" ? 3 : 2;
                b.userpj = userpj == "优" ? 1 : userpj == "差" ? 3 : 2;
                b.show = show == "显示" ? 1 : 2;
                b.pltime = DateTime.Now;
                if (db.SaveChanges() == 1)
                {
                    return Helper.WebRedirect("操作成功！", "history.go(-1);", "操作成功!");
                }
                else
                {
                    return Helper.WebRedirect("操作失败,请检查录入的数据！", "history.go(-1);", "操作失败,请检查录入的数据!");
                }
            }
        }
        public ActionResult pllist()
        {
            return View();
        }

        [HttpPost]
        public ActionResult apllist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;
            using (EFDB db = new EFDB())
            {
                string stime = paramJson["stime"].ToString();
                string etime = paramJson["etime"].ToString();
                string account = paramJson["account"].ToString();
                string name = paramJson["name"].ToString();
                Guid? usergid = null;
                if (!string.IsNullOrEmpty(paramJson["usergid"].ToString()))
                {
                    usergid = Guid.Parse(paramJson["usergid"].ToString());
                }
                Guid? cpgid = null;
                if (!string.IsNullOrEmpty(paramJson["cpgid"].ToString()))
                {
                    cpgid = Guid.Parse(paramJson["cpgid"].ToString());
                }
                Guid? ddgid = null;
                if (!string.IsNullOrEmpty(paramJson["ddgid"].ToString()))
                {
                    ddgid = Guid.Parse(paramJson["ddgid"].ToString());
                }
                var b = db.PL.Select(l => new
                {
                    l.gid,
                    l.usergid,
                    l.cpgid,
                    l.ddgid,
                    l.msg,
                    l.tomsg,
                    l.addtime,
                    l.pltime,
                    l.shoppj,
                    l.userpj,
                    db.LUser.Where(u => u.gid == l.usergid).FirstOrDefault().account,
                    db.ShopList.Where(u => u.gid == l.cpgid).FirstOrDefault().name
                }).AsQueryable();
                if (usergid != null)
                {
                    b = b.Where(l => l.usergid == usergid);
                }
                if (cpgid != null)
                {
                    b = b.Where(l => l.cpgid == cpgid);
                }
                if (ddgid != null)
                {
                    b = b.Where(l => l.ddgid == ddgid);
                }
                if (!string.IsNullOrEmpty(account))
                {
                    b = b.Where(l => l.account.Contains(account));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数
                return Json(new AjaxResult(new
                {
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public JsonResult pldelete(Guid gid)
        {
            if (gid != null)
            {
                using (EFDB db = new EFDB())
                {
                    if (db.PL.Where(l => l.gid == gid).Delete() == 1)
                    {
                        return Json(new AjaxResult("成功"));
                    }
                    else
                    {
                        return Json(new AjaxResult(300, "失败"));
                    }
                }
            }
            else
            {
                return Json(new AjaxResult(300, "非法参数"));
            }
        }
        #endregion

        #region 财务报表
        /// <summary>
        /// 报表
        /// </summary>
        /// <returns>返回调用结果</returns>
        /// <para name="result">200 是成功其他失败</para>
        /// <para name="data">对象结果</para>
        /// <remarks>
        /// 2018-08-18 林建生
        /// </remarks>
        public ActionResult baobiao()
        {
            string stime = ViewBag.stime = Request.Form["stime"];
            string etime = ViewBag.etime = Request.Form["etime"];
            //时间查询
            DateTime? st = DateTime.Now;
            DateTime? et = DateTime.Now;
            if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
            {
                if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                {
                    st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                    et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                }
                else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                {
                    st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                    et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                }
                else
                {
                    st = DateTime.Parse(stime);
                    et = DateTime.Parse(etime);
                }
            }
            using (EFDB db = new EFDB())
            {
                //订单
                var order = db.LOrder.Where(l => l.pay_status == 1);
                if (order.Count() > 0)
                {
                    ViewBag.order = order.Sum(l => l.rmb);
                }
                order = order.Where(l => l.addtime >= st && l.addtime <= et);
                if (order.Count() > 0)
                {
                    ViewBag.ordertime = order.Sum(l => l.rmb);
                }
                //商家
                var shop = db.Pay.Where(l => l.pay_status != 3 && l.lx == 3);
                if (shop.Count() > 0)
                {
                    ViewBag.shop = shop.Sum(l => l.rmb);
                }
                shop = shop.Where(l => l.addtime >= st && l.addtime <= et);
                if (shop.Count() > 0)
                {
                    ViewBag.shoptime = shop.Sum(l => l.rmb);
                }
                //会员
                var user = db.Pay.Where(l => l.pay_status != 3 && l.lx != 3);
                if (user.Count() > 0)
                {
                    ViewBag.user = user.Sum(l => l.rmb);
                }
                user = user.Where(l => l.addtime >= st && l.addtime <= et);
                if (user.Count() > 0)
                {
                    ViewBag.usertime = user.Sum(l => l.rmb);
                }
                //提现
                var pay = db.Pay.Where(l => l.pay_status != 3);
                if (pay.Count() > 0)
                {
                    ViewBag.pay = pay.Sum(l => l.rmb);
                }
                pay = pay.Where(l => l.addtime >= st && l.addtime <= et);
                if (pay.Count() > 0)
                {
                    ViewBag.paytime = pay.Sum(l => l.rmb);
                }
                //会员统计
                var cj = db.LUser.Where(l => l.grade == "初级会员");
                ViewBag.cj = cj.Count();
                cj = cj.Where(l => l.addtime >= st && l.addtime <= et);
                ViewBag.cjtime = cj.Count();
                var zj = db.LUser.Where(l => l.grade == "中级会员");
                ViewBag.zj = zj.Count();
                zj = zj.Where(l => l.addtime >= st && l.addtime <= et);
                ViewBag.zjtime = zj.Count();
                var gj = db.LUser.Where(l => l.grade == "高级会员");
                ViewBag.gj = gj.Count();
                gj = gj.Where(l => l.addtime >= st && l.addtime <= et);
                ViewBag.gjtime = gj.Count();
                //商家统计
                var hz = db.ShopList.Where(l => l.grade == "合作型");
                ViewBag.hz = hz.Count();
                hz = hz.Where(l => l.addtime >= st && l.addtime <= et);
                ViewBag.hztime = hz.Count();
                var cy = db.ShopList.Where(l => l.grade == "创业型");
                ViewBag.cy = cy.Count();
                cy = cy.Where(l => l.addtime >= st && l.addtime <= et);
                ViewBag.cytime = cy.Count();
            }
            return View();
        }
        #endregion

        #region 套餐模块
        /// <summary>
        /// 增加编辑
        /// </summary>
        public ActionResult tcau()
        {
            using (EFDB db = new EFDB())
            {
                if (!string.IsNullOrEmpty(Request.QueryString["gid"]))
                {
                    Guid gid = Guid.Parse(Request.QueryString["gid"]);
                    var b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    ViewBag.name = b.name;
                    ViewBag.picture = b.picture;
                    ViewBag.rmb = b.rmb;
                    ViewBag.explain = b.explain;
                    ViewBag.introduce = b.introduce;
                    ViewBag.sort = b.sort;
                    ViewBag.show = b.show;
                    ViewBag.province = b.province;
                    ViewBag.city = b.city;
                    ViewBag.area = b.area;
                    ViewBag.address = b.address;
                    ViewBag.openid = b.openid;
                    ViewBag.qx = b.qx;
                    ViewBag.grade = b.grade;
                    ViewBag.account = b.account;
                    ViewBag.pwd = b.pwd;
                    ViewBag.xfx = b.xfx;
                    ViewBag.xfd = b.xfd;
                    ViewBag.tese = b.tese;
                    ViewBag.zhekou = b.zhekou;
                    ViewBag.fl = b.fl;
                    ViewBag.lxfs = b.lxfs;
                    ViewBag.khh = b.khh;
                    ViewBag.khm = b.khm;
                    ViewBag.kahao = b.kahao;
                    ViewBag.yjrmb = b.yjrmb;
                    ViewBag.payrmb = b.payrmb;
                    var u = db.LUser.Where(l => l.gid == b.usergid).GroupJoin(db.LUser,
                    x => x.usergid,
                    y => y.gid,
                    (x, y) => new
                    {
                        y.FirstOrDefault().account,
                        y.FirstOrDefault().real_name
                    }).FirstOrDefault();
                    if (u != null)
                    {
                        ViewBag.tjr = u.account;
                        ViewBag.uname = u.real_name;
                    }
                }
                else
                {
                    ViewBag.sort = 1;
                    ViewBag.fl = 0;
                    ViewBag.yjrmb = 0;
                    ViewBag.payrmb = 0;
                    ViewBag.qx = "正常";
                }
                return View(db.CPFL.ToList());
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult tcau(Guid? gid)
        {
            using (EFDB db = new EFDB())
            {
                ShopList b;
                string picture = Request.Form["pictureurl"];
                string name = Request.Form["name"];
                string explain = Request.Form["explain"];
                string introduce = Request.Form["introduce"];
                int sort = Int32.Parse(Request.Form["sort"]);
                string show = Request.Form["show"];
                string province = Request.Form["province"];
                string city = Request.Form["city"];
                string area = Request.Form["area"];
                string address = Request.Form["address"];
                string openid = Request.Form["openid"];
                string grade = Request.Form["grade"];
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                int xfx = Int32.Parse(Request.Form["xfx"]);
                int xfd = Int32.Parse(Request.Form["xfd"]);
                string tese = Request.Form["tese"];
                string zhekou = Request.Form["zhekou"];
                decimal fl = decimal.Parse(Request.Form["fl"]);
                string lxfs = Request.Form["lxfs"];
                int yjrmb = Int32.Parse(Request.Form["yjrmb"]);
                int payrmb = Int32.Parse(Request.Form["payrmb"]);
                string khh = Request.Form["khh"];
                string khm = Request.Form["khm"];
                string kahao = Request.Form["kahao"];
                string qx = Request.Form["qx"];
                Guid flgid = Guid.Parse(Request.Form["flgid"]);
                if (db.ShopList.Where(l => l.name == name && l.gid != gid).Count() > 0)
                {
                    return Helper.WebRedirect("名称已存在！", "history.go(-1);", "商家名称已存在");
                }
                else
                {
                    if (gid == null)
                    {
                        LUser user = new LUser();
                        user.gid = Guid.NewGuid();
                        user.addtime = DateTime.Now;
                        user.account = account;
                        user.real_name = account;
                        user.mobile = account;
                        user.nickname = "商家" + account.Substring(5, 6);
                        user.pwd = Common.MD5.GetMD5ljsheng(pwd);
                        user.usergid = null;
                        user.province = province;
                        user.city = city;
                        user.area = area;
                        user.sex = 0;
                        user.rmb = 0;
                        user.qx = qx;
                        user.loginid = "0000000000";
                        user.lx = 3;
                        user.khh = khh;
                        user.khm = khm;
                        user.kahao = kahao;
                        db.LUser.Add(user);
                        if (db.SaveChanges() != 1)
                        {
                            //会员注册失败
                            db.LUser.Where(l => l.gid == user.gid).Delete();
                            return Helper.WebRedirect("注册失败", "history.go(-1);", "建立基本账户失败!");
                        }
                        else
                        {
                            b = new ShopList();
                            b.gid = Guid.NewGuid();
                            b.addtime = DateTime.Now;
                            b.usergid = user.gid;
                            b.account = account;
                            b.loginid = "0000000000";
                            b.grade = "";
                            b.rmb = 0;
                        }
                    }
                    else
                    {
                        b = db.ShopList.Where(l => l.gid == gid).FirstOrDefault();
                    }
                    if (!string.IsNullOrEmpty(picture))
                    {
                        b.picture = picture;
                    }
                    b.flgid = flgid;
                    b.name = name;
                    b.explain = explain;
                    b.introduce = introduce;
                    b.sort = sort;
                    if (b.pwd != pwd)
                    {
                        b.pwd = Common.MD5.GetMD5ljsheng(pwd);
                    }
                    b.province = province;
                    b.city = city;
                    b.area = area;
                    if (!string.IsNullOrEmpty(picture))
                    {
                        b.picture = picture;
                    }
                    b.address = address;
                    b.openid = openid;
                    b.qx = qx;
                    if (Request.Form["lx"] != "3")
                    {
                        b.grade = grade;
                        b.show = show == "显示" ? 1 : 2;
                    }
                    else
                    {
                        b.show = show == "显示" ? 1 : 2;
                    }
                    b.xfx = xfx;
                    b.xfd = xfd;
                    b.tese = tese;
                    b.zhekou = zhekou;
                    b.fl = fl;
                    b.lxfs = lxfs;
                    b.yjrmb = yjrmb;
                    b.payrmb = payrmb;
                    b.khh = khh;
                    b.khm = khm;
                    b.kahao = kahao;
                    if (gid == null)
                    {
                        db.ShopList.Add(b);
                    }
                    if (db.SaveChanges() == 1)
                    {
                        if (b.pwd != pwd)
                        {
                            string upwd = Common.MD5.GetMD5ljsheng(pwd);
                            string loginid = Common.LCommon.TimeToUNIX(DateTime.Now);
                            db.LUser.Where(l => l.gid == b.usergid).Update(l => new LUser { pwd = upwd, loginid = loginid, khh = khh, khm = khm, kahao = kahao });
                        }
                        else
                        {
                            db.LUser.Where(l => l.gid == b.usergid).Update(l => new LUser { khh = khh, khm = khm, kahao = kahao });
                        }
                        return Helper.WebRedirect("操作成功！", "history.go(-1);", "操作成功!");
                    }
                    else
                    {
                        return Helper.WebRedirect("操作失败,请检查录入的数据！", "history.go(-1);", "操作失败,请检查录入的数据!");
                    }
                }
            }
        }

        // 列表管理
        public ActionResult tclist()
        {
            using (EFDB db = new EFDB())
            {
                ViewBag.cpfl = db.CPFL.Where(l => l.show == 1).ToList();
            }
            return View();
        }

        [HttpPost]
        public ActionResult atclist()
        {
            string json = "";
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                json = HttpUtility.UrlDecode(sr.ReadLine()); //Server.UrlDecode
            }
            //解析参数
            JObject paramJson = JsonConvert.DeserializeObject(json) as JObject;

            using (EFDB db = new EFDB())
            {
                string stime = ViewBag.stime = paramJson["stime"].ToString();
                string etime = ViewBag.etime = paramJson["etime"].ToString();
                string pstime = ViewBag.pstime = paramJson["pstime"].ToString();
                string petime = ViewBag.petime = paramJson["petime"].ToString();
                string name = ViewBag.name = paramJson["name"].ToString();
                Guid? flgid = null;
                if (!string.IsNullOrEmpty(paramJson["flgid"].ToString()) && paramJson["flgid"].ToString()!="0")
                {
                    flgid = ViewBag.flgid = Guid.Parse(paramJson["flgid"].ToString());
                }
                Guid? usergid = null;
                if (!string.IsNullOrEmpty(paramJson["usergid"].ToString()))
                {
                    usergid = ViewBag.usergid = Guid.Parse(paramJson["usergid"].ToString());
                }
                var b = db.ShopList.Where(l => l.show == 1).GroupJoin(db.TC.Where(j => j.usergid == usergid),
                    x => x.gid,
                    y => y.shopgid,
                    (x, y) => new
                    {
                        x.gid,
                        x.sort,
                        x.addtime,
                        x.flgid,
                        x.name,
                        x.xfx,
                        x.xfd,
                        tcgid = y.FirstOrDefault() != null ? y.FirstOrDefault().gid.ToString() : "",
                        rmb = y.FirstOrDefault()!=null?y.FirstOrDefault().rmb:0
                    }).AsQueryable();
                //产品分类
                if (flgid != null)
                {
                    b = b.Where(l => l.flgid == flgid);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    b = b.Where(l => l.name.Contains(name));
                }
                //时间查询
                if (!string.IsNullOrEmpty(stime) || !string.IsNullOrEmpty(etime))
                {
                    DateTime? st = null;
                    DateTime? et = null;
                    if (!string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(stime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else if (string.IsNullOrEmpty(stime) && !string.IsNullOrEmpty(etime))
                    {
                        st = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 00:00:00");
                        et = DateTime.Parse(DateTime.Parse(etime).ToString("yyyy-MM-dd") + " 23:59:59");
                    }
                    else
                    {
                        st = DateTime.Parse(stime);
                        et = DateTime.Parse(etime);
                    }
                    b = b.Where(l => l.addtime >= st && l.addtime <= et);
                }
                //开始分页
                int pageindex = string.IsNullOrEmpty(paramJson["pageindex"].ToString()) ? 1 : Int32.Parse(paramJson["pageindex"].ToString());
                int pagesize = 10;//每页显示的数量
                int count = b.Count();//总行数

                decimal parame1 = 0;
                //if (b.Count() > 0)
                //{
                //    parame1 = b.GroupJoin(db.Pay,
                //    x => x.gid,
                //        y => y.usergid,
                //        (x, y) => new
                //        {
                //            rmb = db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p => p.usergid == x.gid && p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0
                //        }).Sum(l => l.rmb);
                //}
                //decimal parame1 = db.Pay.Where(p => p.lx != 3 && p.addtime >= pst && p.addtime <= pet).Count() > 0 ? db.Pay.Where(p=>p.lx!=3 && p.addtime >= pst && p.addtime <= pet).Sum(p => p.rmb) : 0;
                return Json(new AjaxResult(new
                {
                    parame1,
                    count,
                    pages = Math.Ceiling((double)count / (double)pagesize),
                    pageindex,
                    list = b.Select(l => new
                    {
                        l.gid,
                        l.sort,
                        l.addtime,
                        l.flgid,
                        l.name,
                        l.xfx,
                        l.xfd,
                        l.tcgid,
                        l.rmb
                    }).OrderByDescending(l => l.addtime).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList()
                }));
            }
        }
        #endregion
    }
}